package org.jeecg.tender.keyword.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.security.SecurityUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.tender.keyword.entity.TenderSecurityKeywordManagement;
import org.jeecg.tender.keyword.service.ITenderSecurityKeywordManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: tender_security_keyword_management
 * @Author: jeecg-boot
 * @Date:   2025-02-26
 * @Version: V1.0
 */
@Api(tags="tender_security_keyword_management")
@RestController
@RequestMapping("/keyword/tenderSecurityKeywordManagement")
@Slf4j
public class TenderSecurityKeywordManagementController extends JeecgController<TenderSecurityKeywordManagement, ITenderSecurityKeywordManagementService> {
	@Autowired
	private ITenderSecurityKeywordManagementService tenderSecurityKeywordManagementService;
	@Autowired
	private ISysUserDepartService sysUserDepartService;
	@Autowired
	private ISysDepartService sysDepartService;

	/**
	 * 分页列表查询
	 *
	 * @param tenderSecurityKeywordManagement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "tender_security_keyword_management-分页列表查询")
	@ApiOperation(value="tender_security_keyword_management-分页列表查询", notes="tender_security_keyword_management-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<TenderSecurityKeywordManagement>> queryPageList(TenderSecurityKeywordManagement tenderSecurityKeywordManagement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		String userId = SecurityUtil.getUserId();
		List<DepartIdModel> departIdModels = sysUserDepartService.queryDepartIdsOfUser(userId);
		if(CollectionUtils.isEmpty(departIdModels)){
			return Result.error("没有部门信息");
		}
		QueryWrapper<TenderSecurityKeywordManagement> queryWrapper = new QueryWrapper<>();
		// 添加部门 ID 过滤条件
		List<String> departIds = departIdModels.stream().map(DepartIdModel::getKey).collect(Collectors.toList());
		if (!departIds.isEmpty()) {
			queryWrapper.in("sub_project", departIds);
		}
		// 如果你想对某个字段进行like查询，可以通过queryWrapper加入条件
		if (tenderSecurityKeywordManagement.getKeywordName() != null) {
			queryWrapper.like("keyword_name", tenderSecurityKeywordManagement.getKeywordName());
		}
		if (tenderSecurityKeywordManagement.getSubProject() != null) {
			queryWrapper.like("sub_project", tenderSecurityKeywordManagement.getSubProject());
		}
		// 按 create_time 降序排序
		queryWrapper.orderByDesc("create_time");
		Page<TenderSecurityKeywordManagement> page = new Page<TenderSecurityKeywordManagement>(pageNo, pageSize);
		IPage<TenderSecurityKeywordManagement> pageList = tenderSecurityKeywordManagementService.page(page, queryWrapper);
		pageList.getRecords().forEach(record -> {
			SysDepart sysDepart = sysDepartService.getById(record.getSubProject());
			record.setSubProjectName(null ==sysDepart ? "" : sysDepart.getDepartName());
		});

		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param tenderSecurityKeywordManagement
	 * @return
	 */
	@AutoLog(value = "tender_security_keyword_management-添加")
	@ApiOperation(value="tender_security_keyword_management-添加", notes="tender_security_keyword_management-添加")
	@RequiresPermissions("keyword:tender_security_keyword_management:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TenderSecurityKeywordManagement tenderSecurityKeywordManagement) {
		tenderSecurityKeywordManagementService.save(tenderSecurityKeywordManagement);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tenderSecurityKeywordManagement
	 * @return
	 */
	@AutoLog(value = "tender_security_keyword_management-编辑")
	@ApiOperation(value="tender_security_keyword_management-编辑", notes="tender_security_keyword_management-编辑")
	@RequiresPermissions("keyword:tender_security_keyword_management:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TenderSecurityKeywordManagement tenderSecurityKeywordManagement) {
		tenderSecurityKeywordManagementService.updateById(tenderSecurityKeywordManagement);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "tender_security_keyword_management-通过id删除")
	@ApiOperation(value="tender_security_keyword_management-通过id删除", notes="tender_security_keyword_management-通过id删除")
	@RequiresPermissions("keyword:tender_security_keyword_management:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		tenderSecurityKeywordManagementService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "tender_security_keyword_management-批量删除")
	@ApiOperation(value="tender_security_keyword_management-批量删除", notes="tender_security_keyword_management-批量删除")
	@RequiresPermissions("keyword:tender_security_keyword_management:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tenderSecurityKeywordManagementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "tender_security_keyword_management-通过id查询")
	@ApiOperation(value="tender_security_keyword_management-通过id查询", notes="tender_security_keyword_management-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TenderSecurityKeywordManagement> queryById(@RequestParam(name="id",required=true) String id) {
		TenderSecurityKeywordManagement tenderSecurityKeywordManagement = tenderSecurityKeywordManagementService.getById(id);
		if(tenderSecurityKeywordManagement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tenderSecurityKeywordManagement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tenderSecurityKeywordManagement
    */
    @RequiresPermissions("keyword:tender_security_keyword_management:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TenderSecurityKeywordManagement tenderSecurityKeywordManagement) {
        return super.exportXls(request, tenderSecurityKeywordManagement, TenderSecurityKeywordManagement.class, "tender_security_keyword_management");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("keyword:tender_security_keyword_management:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TenderSecurityKeywordManagement.class);
    }

}
